package com.example.zg3_park.User

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.mvvm.BaseActivity
import com.example.lib_base.net.Const
import com.example.zg3_park.R
import com.example.zg3_park.databinding.ActivityLoginBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/LoginActivity")
class LoginActivity : BaseActivity<ActivityLoginBinding,UserViewModel>() {

    override fun intiView() {
        val loginTime = SPUtils.getInstance().getLong("loginTime")
        val currentTime = System.currentTimeMillis()
        val day = (currentTime - loginTime)/1000/60/60/24
        if(day < 7) {
            start()
        }

        binding.btnLogin.setOnClickListener {
            val name = binding.loginUname.text.toString()
            val pwd = binding.loginPass.text.toString()
            if(name.isNullOrEmpty() || pwd.isNullOrEmpty()) {
                ToastUtils.showLong("输入内容不能为空")
                return@setOnClickListener
            }

//            if(!name.matches(Regex("[A-z0-9_]{2,8}"))) {
//                ToastUtils.showLong("不是合法用户名")
//                return@setOnClickListener
//            }
            vm.login(name,pwd)
        }
    }
    fun start() {
        if(SPUtils.getInstance().getInt(Const.ROLE,-1) == 11) {
            ARouter.getInstance().build("/app/MainActivity").navigation()
        }else {
            ARouter.getInstance().build("/app/CarTypeActivity").navigation()
        }
        finish()
    }

    override fun intiData() {
        vm.loginData.observe(this) {
            Log.i("lr",it.toString())
            SPUtils.getInstance().apply {
                put(Const.TOKEN,it.token)
                put(Const.USERNAME,it.username)
                put(Const.UID,it.id)
                put("loginTime",System.currentTimeMillis())
                put(Const.ROLE,it.roleid)   //角色
            }
           start()
        }
    }

}